package com.bawei.shoppingcar.listviewfood_demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.bawei.shoppingcar.R;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class FoodActivity extends AppCompatActivity {

    private ListView lv;//控件
    private List<FoodEntity.DataBean> list = new ArrayList<>();//数据源
    private FoodAdapter adapter;//适配器
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //TODO 3.主线程解析json数据：List集合 data数据源
            if(msg.what == 101){
                String json = (String) msg.obj;
                Toast.makeText(FoodActivity.this, ""+json, Toast.LENGTH_SHORT).show();
                FoodEntity foodEntity = new Gson().fromJson(json, FoodEntity.class);
                List<FoodEntity.DataBean> data = foodEntity.getData();//
                list.addAll(data);//将网络请求到的数据添加数据源中
                adapter.notifyDataSetChanged();//刷新适配器
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        lv = findViewById(R.id.lv);
        adapter = new FoodAdapter(list,FoodActivity.this);
        lv.setAdapter(adapter);//设置适配器
        new FoodThread().start();

        //点击
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(FoodActivity.this, ""+list.get(i).getFood_str(), Toast.LENGTH_SHORT).show();
            }
        });
        //长按
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                list.remove(i);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }
    //TODO 1.网络请求数据：String json
    class FoodThread extends Thread{
        @Override
        public void run() {
            super.run();
            try {
                URL url = new URL("http://43.143.146.165:8181/foods/getFoods?currentPage=1&pageSize=12");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("GET");//注意必须是大写
                httpURLConnection.setConnectTimeout(10*1000);
                httpURLConnection.setReadTimeout(10*1000);
                httpURLConnection.connect();
                if(httpURLConnection.getResponseCode() == 200){
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder stringBuilder = new StringBuilder();
                    String str = "";
                    while ((str = bufferedReader.readLine())!= null){
                        stringBuilder.append(str);
                    }
                    String json = stringBuilder.toString();
                    //TODO 2.使用handler发送数据主线程

                    Message message = Message.obtain();
                    message.what = 101;
                    message.obj = json;
                    handler.sendMessage(message);

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}